//
//  WPNewSecondTVC.m
//  wobingwoyi
//
//  Created by WGP on 16/11/10.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewSecondTVC.h"
#import "ZNBPanNavigationController.h"
#import "WPNewTreamentInstructionCell.h"
#import "WPNewTreatmentInstructionsEditeVC.h"
#import "WPDiseaseCaseHeaderView.h"
#import "WPMRModel.h"
#import "WPTreamentProgressListModel.h"
#import "WPFirstTableFooterView.h"
#import "WPTreamentAddNewVisitVC.h"
#import "WPViewControllerNewTreamentFirstEditeVC.h"

@interface WPNewSecondTVC ()

@property(nonatomic,strong)NSMutableArray *models;
@property(nonatomic,strong)WPFirstTableFooterView *footerView;

@end

@implementation WPNewSecondTVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.bounces = NO;
    self.tableView.backgroundColor = GlobalBgColor;

    [self.tableView registerClass:[WPDiseaseCaseHeaderView class] forHeaderFooterViewReuseIdentifier:@"WPDiseaseCaseHeaderView"];
    [self.tableView registerClass:[WPNewTreamentInstructionCell class] forCellReuseIdentifier:@"WPNewTreamentInstructionCell"];
     [self.tableView registerClass:[WPNewTreamentInstructionCell class] forCellReuseIdentifier:@"WPNewTreamentInstructionCell1"];
    
    //头部
    WPDiseaseCaseHeaderView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPDiseaseCaseHeaderView"];
    headerView.index = 0;
    headerView.frame= CGRectMake(0, 0, SCREEN_WIDTH, 44);
    self.tableView.tableHeaderView = headerView;
    
    //尾部
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 69)];
    bgView.backgroundColor = GlobalBgColor;
    WPFirstTableFooterView *footerView = [[WPFirstTableFooterView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 44)];
    footerView.addTitle = @"添加治疗过程";
    [bgView addSubview:footerView];
    
    __weak typeof(self) weakSelf = self;
    footerView.addDiseaseBlick = ^{
        
        if (!self.models.count)
        {
            [self showInfoStatus:@"请先补充一诊信息"];
            return ;
        }
        
        //添加新的诊断
        WPTreamentAddNewVisitVC *vc = [WPTreamentAddNewVisitVC new];
        vc.reloadBlock = ^{
            
            [weakSelf.models removeAllObjects];
            [weakSelf.models addObjectsFromArray:weakSelf.model.processDescp_list];
            if (self.models.count)
            {
                self.footerView.hidden = NO;
            }
            else
            {
                self.footerView.hidden = YES;
            }
            [weakSelf.tableView reloadData];
        };
        vc.rank = weakSelf.models.count + 2;
        vc.model = weakSelf.model;
        
        
        ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
        [weakSelf presentViewController:nav animated:YES completion:nil];
    };
    
    self.tableView.tableFooterView = bgView;
    self.tableView.allowsSelection = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.models addObjectsFromArray:self.model.processDescp_list];
    
//    if (self.models.count)
//    {
//        footerView.hidden = NO;
//    }
//    else
//    {
//        footerView.hidden = YES;
//    }
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        if (self.models.count)
        {
            return self.models.count;
        }
        else
        {
            return 1;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0 && indexPath.section == 0)
    {
        
            WPNewTreamentInstructionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPNewTreamentInstructionCell"];
            cell.descpTime = self.model.descpTime;
            cell.descpStr = self.model.treamentProcessStr;
            cell.imagesArr = self.model.treamentProcessArr;
            cell.isVisit = NO;
            cell.index = indexPath.item;
            
            cell.editeBtnBlock = ^{
                
                BOOL isVisit = NO;
                
                WPViewControllerNewTreamentFirstEditeVC *vc = [WPViewControllerNewTreamentFirstEditeVC new];
                vc.reloadBlock = ^{
                    
                    [weakSelf.tableView reloadData];
                };
                vc.visitIndex = indexPath.item;
                vc.isVisit = isVisit;
                vc.model = weakSelf.model;
                vc.rank = 1;
                
                ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
                [weakSelf presentViewController:nav animated:YES completion:nil];
            };
            return cell;
    }
    else
    {
        WPTreamentProgressListModel *returnList = nil;
        if (self.models.count)
        {
            returnList = self.models[indexPath.item];
        }
        
            WPNewTreamentInstructionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPNewTreamentInstructionCell1"];
            cell.descpTime = returnList.writeTime;
            cell.descpStr = returnList.descpStr;
            cell.imagesArr = returnList.descpURLS;
            cell.isVisit = YES;
            cell.index = indexPath.item;
            
            cell.editeBtnBlock = ^{
                
                BOOL isVisit = YES;
                
                WPNewTreatmentInstructionsEditeVC *vc = [WPNewTreatmentInstructionsEditeVC new];
                vc.reloadBlock = ^{
                    
                    [weakSelf.models removeAllObjects];
                    [weakSelf.models addObjectsFromArray:weakSelf.model.processDescp_list];
                    if (self.models.count)
                    {
                        self.tableView.tableFooterView.hidden = NO;
                    }
                    else
                    {
                        self.tableView.tableFooterView.hidden = YES;
                    }
                    [weakSelf.tableView reloadData];
                };
                vc.visitIndex = indexPath.item;
                vc.isVisit = isVisit;
                vc.rank = indexPath.item + 2;
                vc.model = weakSelf.model;
                
                ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
                [weakSelf presentViewController:nav animated:YES completion:nil];
            };
            return cell;
        }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
    CGFloat itemHeight = itemWidth*90/112.5;
    if (indexPath.item == 0)
    {
        NSString *descpStr = self.model.treamentProcessStr;
        CGSize size = [descpStr sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) font:15];
        CGFloat cellHeight = size.height + itemHeight + 2 + 84;
        return cellHeight;
    }
    else
    {
        WPTreamentProgressListModel *returnList = nil;
        if (self.models.count)
        {
            returnList = self.models[indexPath.item];
        }
        
        NSString *descpStr = returnList.descpStr;
        CGSize size = [descpStr sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) font:15];
        return itemHeight+2 + size.height + 84;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 44;
    }
    else
    {
        return 0.0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 1)
    {
        WPDiseaseCaseHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPDiseaseCaseHeaderView"];
        headerView.index = section;
        return headerView;
    }
    else
    {
        return nil;
    }
}

-(NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}
@end
